package com.common.handler;

import com.beans.ErrorStatusEnum;
import com.common.entity.MyException;
import com.common.entity.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import java.nio.file.AccessDeniedException;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * @author gm
 * @description 全局异常捕获
 * @date 2020/1/10
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 自定义异常处理
     *
     * @param e
     * @return com.common.entity.R
     * @author gm
     * @date 2020/5/30
     */
    @ResponseBody
    @ExceptionHandler(MyException.class)
    public R exceptionHandler(MyException e) {
        log.error(e.getMessage(), e);
        return R.error(e.getCode(), e.getMessage());
    }

    /**
     * @param e
     * @return com.common.entity.R
     * @Valid 参数校验异常处理
     * @author gm
     * @date 2020/5/30
     */
    @ResponseBody
    @ExceptionHandler(BindException.class)
    public R exceptionHandler(BindException e) {
        e.printStackTrace();
        log.error(Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage());
        List errorList = new LinkedList<>();
        for (FieldError objectError : e.getBindingResult().getFieldErrors()) {
            errorList.add("Field:   " + objectError.getField());
            errorList.add("Error: " + objectError.getDefaultMessage());
        }
        return R.error(ErrorStatusEnum.PARAMS_ILLEGAL_ERROR.code(), ErrorStatusEnum.PARAMS_ILLEGAL_ERROR.message(), errorList);
    }

    /**
     * 权限验证异常处理
     *
     * @param e
     * @return com.common.entity.R
     * @author gm
     * @date 2020/5/30
     */
    @ResponseBody
    @ExceptionHandler(AccessDeniedException.class)
    public R exceptionHandler(AccessDeniedException e) {
        log.error(e.getMessage(), e);
        return R.error(ErrorStatusEnum.NOT_AUTHORITY.code(), ErrorStatusEnum.NOT_AUTHORITY.message());
    }

    /**
     * 单个文件上传超过2M大小限制
     *
     * @param e
     * @return com.common.entity.R
     * @author gm
     * @date 2020/5/30
     */
    @ResponseBody
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public R exceptionHandler(MaxUploadSizeExceededException e) {
        log.error("单个文件上传超过2M大小限制");
        return R.error(ErrorStatusEnum.UPLOAD_FILE_ERROR.code(), "上传文件大小请勿超过2M!");
    }


    /**
     * 其他异常处理
     *
     * @param e
     * @return com.common.entity.R
     * @author gm
     * @date 2020/5/30
     */
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public R exceptionHandler(Exception e) {
        log.error(e.getMessage(), e);
        e.printStackTrace();
        return R.error(500, e.getMessage());
    }
}
